package com.example.http.code2xx;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * 测试HTTP协议 关于2xx返回的信息
 *
 * @author zhouxl
 * @DATE 2021/1/21
 * @descriptoin
 */
@Controller
@RequestMapping("api/code")
public class Code2xx {

    /**
     * 状态码返回200数据信息
     *
     * @return java.lang.Object
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("code200")
    @ResponseBody
    public Object code200(HttpServletRequest request, HttpServletResponse response) {
        response.setStatus(200);
        return "200";
    }

    /**
     * 状态码返回204数据信息
     *
     * @return java.lang.Object
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("code204")
    @ResponseBody
    public Object code204(HttpServletRequest request, HttpServletResponse response) {
        response.setStatus(204);
        return "204";
    }

    /**
     * 测试失败了
     * 了解该状态码地址：https://my.oschina.net/u/4255011/blog/3709376
     * https://www.jb51.net/article/105168.htm
     * 状态码返回206数据信息
     *
     * @return java.lang.Object
     * @author zhouxl
     * @date 2021-01-21
     */
    @RequestMapping("code206")
    @ResponseBody
    public Object code206(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String str = "helloworld,hyeskkdlskfdjslkdfjlsksdjflskfjkls";
        response.setStatus(206);
//        response.setHeader("Content-Range","bytes 0-100");
        response.setContentType("application/octet-stream");
        FileInputStream fis = new FileInputStream("D:\\apache_ab\\Apache24\\CHANGES.txt");
        IOUtils.copy(fis, response.getOutputStream());
        fis.close();
        response.getOutputStream().flush();
        return null;
    }

}
